#include<iostream>
#include<cassert>
#include<cstring>

namespace MY_Vecter
{
    template<typename T>
    class myvecter
    {
    public:
        typedef T* itertor;
        typedef T value_type;
        //定义迭代器
        itertor begin()
        {
            return _start;
        }
        itertor end()
        {
            return _finish;
        }
        //构造函数 
        myvecter(int capacity = 0)
        :_start(nullptr)
        ,_finish(nullptr)
        ,_endOfStorage(nullptr)
        {
            assert(capacity >= 0);
            //如果capacity为0，则不做操作
            if(capacity == 0)
                return;
            _start = new value_type[capacity];
            assert(_start);
            _finish = _start;
            _endOfStorage = _start + capacity;
        }
        //返回vector的容量
        size_t capacity(void)
        {
            return _endOfStorage - _start;
        }
        //返回vector的大小
        size_t size(void)
        {
            return _finish - _start;
        }
        //尾插
        void push_back(const value_type& val)
        {
            //判断容量是否够
            if (size() == capacity())
                add_capacity(capacity() * 2);

            *_finish = val;
            _finish++;
        }
        //尾删
        void pop_back(void)
        {
            _finish--;
        }
        //改变容量
        void reverse(size_t newcapacity)
        {
            assert(newcapacity >= 0);

            if (newcapacity > capacity())
                add_capacity(newcapacity);
        }
    private:
        //扩容
        void add_capacity(int newcapacity)
        {
            assert(newcapacity > capacity());
            //new一个新的区域
            value_type* tem = _start;
            size_t sz = size();
            _start = new value_type[newcapacity];
            assert(_start);
            //复制数据
            memcpy(_start, tem, sizeof(value_type) * sz);
            //更改变量
            _endOfStorage = _start + newcapacity;
            _finish = _start + sz;
        }
    private:
        value_type* _start; //指向vector开头
        value_type* _finish; //指向vector数据的结尾
        value_type* _endOfStorage; //指向vector的末尾

    };
}

